package com.demomaster.quickcompat.widget;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class QuickCheckBoxLayout extends FrameLayout {
    private static final String TAG = "QuickCheckBoxLayout";

    public QuickCheckBoxLayout(@NonNull Context context) {
        super(context);
    }

    public QuickCheckBoxLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public QuickCheckBoxLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public QuickCheckBoxLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        init();
    }

    private void init() {
        if (getId() == View.NO_ID) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                setId(View.generateViewId());
            }
        }
    }

    QuickCheckBoxGroup.OnCheckedChangeListener mOnCheckedChangeListener;
    public void setOnCheckedChangeListener(QuickCheckBoxGroup.OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }
    public boolean onInterceptCheckEvent(View view,boolean isChecked){
        return false;
    }
    QuickCheckBoxGroup.OnCheckedChangeListener innerOnCheckedChangeListener = new QuickCheckBoxGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(ViewGroup group, boolean isChecked, int changedViewId, QuickCheckBoxLayout quickCheckBoxLayout) {
            onChildCheckedChanged(group,isChecked,changedViewId,quickCheckBoxLayout);
        }
    };

    public void onChildCheckedChanged(ViewGroup group, boolean isChecked, int changedViewId, QuickCheckBoxLayout quickCheckBoxLayout){
        View view = group.findViewById(changedViewId);
        String viewText = null;
        if(view instanceof CompoundButton){
            CompoundButton compoundButton = (CompoundButton) view;
            viewText = TextUtils.isEmpty(compoundButton.getText())?"nul":compoundButton.getText().toString();
        }

        Log.i(TAG,viewText + ",id=" + ((View) group.getParent()).getId()+",isChecked="+isChecked);
        //(CompoundButton buttonView, boolean isChecked)
        if(onInterceptCheckEvent(group.findViewById(changedViewId),isChecked)){
            Log.i(TAG,"拦截选中事件=" + QuickCheckBoxLayout.this.isChecked);
            if (group.findViewById(changedViewId) instanceof CompoundButton) {
                ((CompoundButton) group.findViewById(changedViewId)).setChecked(QuickCheckBoxLayout.this.isChecked);
            } else if (group.findViewById(changedViewId) instanceof QuickCheckBoxLayout) {
                ((QuickCheckBoxLayout) group.findViewById(changedViewId)).setChecked(QuickCheckBoxLayout.this.isChecked);
            }
            return;
        }
        //Log.i(TAG,"不拦截选中事件,当前选中状态" + QuickCheckBoxLayout.this.isChecked+",新的选中状态："+isChecked);
        QuickCheckBoxLayout.this.isChecked = isChecked;
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged((ViewGroup) QuickCheckBoxLayout.this.getParent(), isChecked, changedViewId, QuickCheckBoxLayout.this);
        }
    }
    private boolean isChecked;
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View childView = getFirstCheckedView(this);
        //Log.i(TAG,"setOnCheckedChangeListener childView=" + childView);
        if (childView instanceof CompoundButton) {
            ((CompoundButton) childView).setOnCheckedChangeListener(innerOnCheckedChangeListener);
            isChecked = ((CompoundButton) childView).isChecked();
        } else if (childView instanceof QuickCheckBoxLayout) {
            ((QuickCheckBoxLayout) childView).setOnCheckedChangeListener(innerOnCheckedChangeListener);
            isChecked = ((QuickCheckBoxLayout) childView).isChecked();
        }
    }

    public View getFirstCheckedView(ViewGroup view) {
            int count = view.getChildCount();
            for (int i = 0; i < count; i++) {
                View childView = view.getChildAt(i);
                if (childView instanceof CompoundButton) {
                    return childView;
                } else if (childView instanceof QuickCheckBoxLayout) {
                    return childView;
                } else if (childView instanceof ViewGroup) {
                    View view1 = getFirstCheckedView((ViewGroup) childView);
                    if (view1 != null) {
                        return view1;
                    }
                }
            }
        return null;
    }

    public boolean isChecked() {
        View childView = getFirstCheckedView(this);
        if (childView instanceof CompoundButton) {
           return ((CompoundButton) childView).isChecked();
        } else if (childView instanceof QuickCheckBoxLayout) {
            return ((QuickCheckBoxLayout) childView).isChecked();
        }
        return false;
    }
    public void setChecked(boolean checked) {
        View childView = getFirstCheckedView(this);
        if (childView instanceof CompoundButton) {
            CompoundButton compoundButton = ((CompoundButton) childView);
            Log.i(TAG,"setChecked=" + checked+",txt="+compoundButton.getText());
            compoundButton.setChecked(checked);
        } else if (childView instanceof QuickCheckBoxLayout) {
            //Log.i(TAG,"setChecked2=" + checked);
            ((QuickCheckBoxLayout) childView).setChecked(checked);
        }else {
            Log.e(TAG,"setChecked=" + checked+",childView="+childView);
        }
    }
    public void setDefChecked(boolean checked){
        QuickCheckBoxLayout.this.isChecked = checked;
        setChecked(checked);
    }
}
